package com.lance.answer.gui.answer.gk.answer.lms;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.DealJsonString;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.CourseConfig;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoAnswerTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

public class CircleDoAnswerForGetAnswer {


    public static void main(String[] args) throws Exception {

        String driverPath = "d:\\ynouAutoStudy\\chromedriver.exe";
        ChromeDriver driver = Util.getDriver(driverPath);
        String url = "https://menhu.pt.ouchn.cn/site/ouchnPc/index";
        driver.get(url);
        Thread.sleep(3000);

        String userName = "2165001457036";
        String pwd = "Ouchn@2021";
        LmsUtil.login(driver,userName,pwd);

        List<String> choices = Lists.newArrayList();
        choices.add("A");
        choices.add("B");
        choices.add("C");
        choices.add("D");

        String doExamUrl = "https://lms.ouchn.cn/course/10000003892/learning-activity/full-screen#/exam/10000067002";

        for(String choice :choices){

            //      String doExamUrl = "https://lms.ouchn.cn/course/10000000714/learning-activity/full-screen#/exam/10000006125";
            driver.get(doExamUrl);
            try {
                Thread.sleep(10 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 确认开始答题
            DoAnswerTask.confirmDoAnswer(driver);

            // 找到所有的问题element
            Thread.sleep(5000);
            List<WebElement> webElements = DoAnswerTask.findWeElements(driver, "li[ng-class='subject.type']", 20);
            List<WebElement> questionElements = Lists.newLinkedList();

            webElements.forEach(e -> {
                if (!e.getAttribute("class").equals("subject ng-scope text")) {
                    questionElements.add(e);
                }
            });

            // 开始答题
            for (WebElement questionWe : questionElements) {

                for (WebElement singleAnswerElement : questionWe.findElements(By.cssSelector("li[class='option ng-scope vertical']"))) {

                    WebElement answerWe = singleAnswerElement.findElement(By.cssSelector("span[class='left']"));

                    WebElement cWe = answerWe.findElement(By.cssSelector("span[class='option-index ng-binding']"));

                    if(choice.equals(cWe.getText())){
                        answerWe.click();
                    }
                }

                }

            DoAnswerTask.submitTesting(driver);
            }


        }


    }




